﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace SW.Core
{
    /// <summary>
    /// 安全辅助类
    /// </summary>
    public class SecurityHelper
    {
        private static byte[] arrDESKey;
        private static byte[] arrDESIV;

        /// <summary>
        /// 用于对称算法的密钥。
        /// </summary>
        public static byte[] _ArrDESKey
        {
            get
            {
                if (arrDESKey != null && arrDESKey.Length > 0)
                {
                    string sKey = Config.Security.Key;
                    if (String.IsNullOrEmpty(sKey))
                        sKey = "Strong";
                    arrDESKey = Encoding.UTF8.GetBytes(sKey);
                }
                return arrDESKey;
            }
        }

        /// <summary>
        /// 用于对称算法的初始化向量。
        /// </summary>
        public static byte[] _ArrDESIV
        {
            get
            {
                if (arrDESIV != null && arrDESIV.Length > 0)
                {
                    string sIV = Config.Security.IV;
                    if (String.IsNullOrEmpty(sIV))
                        sIV = "World";
                    arrDESIV = Encoding.UTF8.GetBytes(sIV);
                }
                return arrDESIV;
            }
        }

        /// <summary> 
        /// 加密数据 
        /// </summary>
        /// <param name="text">加密明文</param>
        public static string DESEncrypt(string text)
        {
            if (String.IsNullOrEmpty(text))
                throw new ArgumentNullException("加密明文为空");

            DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
            MemoryStream objMemoryStream = new MemoryStream();
            CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objDES.CreateEncryptor(_ArrDESKey, _ArrDESIV), CryptoStreamMode.Write);
            StreamWriter objStreamWriter = new StreamWriter(objCryptoStream);
            objStreamWriter.Write(text);
            objStreamWriter.Flush();
            objCryptoStream.FlushFinalBlock();
            objMemoryStream.Flush();
            return Convert.ToBase64String(objMemoryStream.GetBuffer(), 0, (int)objMemoryStream.Length);
        }

        /// <summary>
        /// 解密。
        /// </summary>
        /// <param name="text">解密密文</param>
        public static string DESDecrypt(string text)
        {
            if (text == null)
                throw new ArgumentNullException("解密密文为空");

            DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
            byte[] arrInput = Convert.FromBase64String(text);
            MemoryStream objMemoryStream = new MemoryStream(arrInput);
            CryptoStream objCryptoStream = new CryptoStream(objMemoryStream, objDES.CreateDecryptor(_ArrDESKey, _ArrDESIV), CryptoStreamMode.Read);
            StreamReader objStreamReader = new StreamReader(objCryptoStream);
            return objStreamReader.ReadToEnd();
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="encyptStr">加密明文</param>
        /// <returns></returns>
        public static string Md5(string encyptStr)
        {
            string retStr;
            MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();
            byte[] inputBye;
            byte[] outputBye;
            inputBye = System.Text.Encoding.ASCII.GetBytes(encyptStr);
            outputBye = m5.ComputeHash(inputBye);
            retStr = Convert.ToBase64String(outputBye);
            return (retStr);
        }
    }
}
